package com.tjsinfo.tjsoa.worklog.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.tjsinfo.tjsoa.system.vo.User;
import com.tjsinfo.tjsoa.worklog.bo.IWorkLogExamineBo;
import com.tjsinfo.tjsoa.worklog.bo.IWorkLogMainBo;
import com.tjsinfo.tjsoa.worklog.dao.IWorkLogUserDAO;
import com.tjsinfo.tjsoa.worklog.sqlmap.UserInfo;
import com.tjsinfo.tjsoa.worklog.sqlmap.WorkLogDefine;
public class AddWorkLogAction extends Action{
	private IWorkLogMainBo logMainBo;
	private IWorkLogExamineBo worklogExamineBo;
	private IWorkLogUserDAO workLogUserDAO;
	public IWorkLogUserDAO getWorkLogUserDAO() {
		return workLogUserDAO;
	}
	public void setWorkLogUserDAO(IWorkLogUserDAO workLogUserDAO) {
		this.workLogUserDAO = workLogUserDAO;
	}


	public IWorkLogMainBo getLogMainBo() {
		return logMainBo;
	}


	public void setLogMainBo(IWorkLogMainBo logMainBo) {
		this.logMainBo = logMainBo;
	}


	public IWorkLogExamineBo getWorklogExamineBo() {
		return worklogExamineBo;
	}


	public void setWorklogExamineBo(IWorkLogExamineBo worklogExamineBo) {
		this.worklogExamineBo = worklogExamineBo;
	}


	/* (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		// TODO Auto-generated method stub
		try {
			String time = request.getParameter("time");
			if (time == null || "".equals(time)) {
				date = new Date();
			} else {
				date = dateFormat.parse(time);	
			}
			Integer subid = 0;
			String shangbaoren = request.getParameter("jubaorenid");
			if(shangbaoren != null&&!"".equals(shangbaoren))
			{
				shangbaoren = shangbaoren.substring(0,shangbaoren.length() - 1);
				
				subid = 1;
			}
			WorkLogDefine	 workLogDefine = new WorkLogDefine();
			workLogDefine.setTjsUserByUserId(user.getUser_id());
			workLogDefine.setType("D");
			List list= workLogUserDAO.selectDefine(workLogDefine);
			
			if (list.size() == 0) {
				subid = 2;
			}
			String jihuaid =  logMainBo.save(user.getUser_id(), date, request.getParameter("type"), request.getParameter("contest"), "",Integer.parseInt(request.getParameter("year")),Integer.parseInt(request.getParameter("number")),1,subid);
			 worklogExamineBo .addShangBaoRen(shangbaoren, jihuaid);
			 WorkLogDefine workLogDefine2 = new WorkLogDefine();
			 workLogDefine2.setTjsUserByUserId(user.getUser_id());
			 workLogDefine2.setType("D");
			List<UserInfo>	userlist = workLogUserDAO.selectDefine(workLogDefine2);
			String[] array = shangbaoren.split(",");
			for (int i = 0; i < array.length; i++) {
				if(!"".equals(array[i]))
				{
				}
			
			}
			 if("D".equals(request.getParameter("type")))
				{
				 	request.setAttribute("linkurl", "tjsinfo/tjsoa/worklog/workLogDateModle.do");
					request.setAttribute("message", "worklog.message.save");
					return mapping.findForward("success");
					
				}
				else if("W".equals(request.getParameter("type")))
				{
					request.setAttribute("linkurl", "tjsinfo/tjsoa/worklog/workLogWeekModle.do");
					request.setAttribute("message", "worklog.message.save");
					return mapping.findForward("success");
					
				}
				else if("M".equals(request.getParameter("type")))
				{
					request.setAttribute("linkurl", "tjsinfo/tjsoa/worklog/workLogMonthModle.do");
					request.setAttribute("message", "worklog.message.save");
					return mapping.findForward("success");
				}
				else if("Q".equals(request.getParameter("type")))
				{
					request.setAttribute("linkurl", "tjsinfo/tjsoa/worklog/workLogJiDuModle.do");
					request.setAttribute("message", "worklog.message.save");
					return mapping.findForward("success");
					
				}
				else if("H".equals(request.getParameter("type")))
				{
					request.setAttribute("linkurl", "tjsinfo/tjsoa/worklog/workLogHalfYearModle.do");
					request.setAttribute("message", "worklog.message.save");
					return mapping.findForward("success");
				}
				else if("Y".equals(request.getParameter("type")))
				{
					request.setAttribute("linkurl", "tjsinfo/tjsoa/worklog/workLogYearModle.do");
					request.setAttribute("message", "worklog.message.save");
					return mapping.findForward("success");
				}	
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return mapping.findForward("error");
	}

}
